from django.db import models
from django.utils import timezone
from datetime import date
# Create your models here.


class zy_p(models.Model):
    GENDER_CHOICES = [
        ("M", "男"),
        ("F", "女"),
    ]
    zyh = models.CharField(max_length=12, primary_key=True, verbose_name="住院号")
    name = models.CharField(max_length=100, verbose_name="姓名")
    gender = models.CharField(
        max_length=1, choices=GENDER_CHOICES, default="M", verbose_name="性别")
    birth = models.DateField(verbose_name="出生日期")
    add = models.CharField(max_length=500, blank=True,
                           null=True, verbose_name="地址")
    tel = models.CharField(max_length=20, null=True, verbose_name="电话")

    @property
    def age(self):
        """计算精确的周岁年龄。"""
        today = date.today()
        # 核心计算逻辑：当前年减去出生年，如果今年生日未过则减1
        return today.year - self.birth.year - ((today.month, today.day) < (self.birth.month, self.birth.day))

    class Meta:
        verbose_name = "病人信息"
        verbose_name_plural = "病人信息"
